﻿Public Class frmProjectsList

    Dim strNameForm As String = "Projects"

    Public Function Initialize() As Boolean
        Try
            Me.Text = strNameForm
            objList.NameList = strNameForm
            objList.NameForm = strNameForm

            objList.AddButtonStandard()
            grdGrid.AddMenuStandard()
            grdGrid.NomeElementoPerEsportazione = "Projects"
            grdGrid.NomeCampoRicercaDaTastiera = "Name"

            Return LoadGrid("")
        Catch ex As Exception
            Base.ShowError.Show("Initialize", Me.Name, "Initialize", ex)
            Return False
        End Try
    End Function

#Region "Grid"

    Private Function LoadGrid(ByVal inSqlWhere As String) As Boolean
        Try
            Dim strSqlWhere As String = IIf(inSqlWhere = "", "", "Where " & inSqlWhere)
            Dim strSql As String = String.Format("" &
                                    "Select      Projects.ID, " &
                                    "            Projects.Name, " &
                                    "            Projects.Description, " &
                                    "            Projects.StartDate " &
                                    "From        Projects " &
                                    "{0} " &
                                    "Order by    StartDate ", strSqlWhere)


            Using daAdapt As New MySqlDataAdapter(strSql, gConnectionStringAgenda.ToString)
                Using dtTable As New DataTable("Projects")
                    daAdapt.Fill(dtTable)
                    grdGrid.DataSource = dtTable
                    grdGrid.Columns("ID").Visible = False

                    grdGrid.RowsDefaultCellStyle.WrapMode = DataGridViewTriState.True
                    grdGrid.ColumnHeadersDefaultCellStyle.WrapMode = DataGridViewTriState.True
                    grdGrid.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells

                    'dtTable.WriteXml(GetSetting(Application.ProductName, "Impostazioni", "PathReport", "") & "\Projects.xml", True)

                    Return True
                End Using
            End Using
        Catch ex As Exception
            Base.ShowError.Show("Load Grid", Me.Name, "LoadGrid", ex)
            Return False
        End Try
    End Function

#End Region

#Region "Commands"

    Private Sub Delete() Handles objList.OnDelete, grdGrid.OnElimina
        Try
            If grdGrid.CurrentRow IsNot Nothing Then
                Me.Cursor = Cursors.WaitCursor
                Me.Enabled = False

                Dim intID As Integer = Utility.ConvStrNum(grdGrid.CurrentRow.Cells("ID").Value)
                Dim oTProjects As New Data.TProjects
                If oTProjects.IsPossibleDelete() Then
                    If oTProjects.Delete(intID) Then
                        Call LoadGrid("")
                    Else
                        MsgBox("Impossible delete selected project.", vbExclamation, Me.Text)
                    End If
                End If
            End If
        Catch ex As Exception
            Base.ShowError.Show("Delete", Me.Name, "Delete", ex)
        Finally
            Me.Cursor = Cursors.Default
            Me.Enabled = True
        End Try
    End Sub

    Private Sub Insert() Handles objList.OnInsert, grdGrid.OnInserisci
        Try
            Me.Cursor = Cursors.WaitCursor
            Me.Enabled = False

            Dim ofrmProjectsInsertion As New frmProjectsInsert
            If ofrmProjectsInsertion.Initialize() Then
                If ofrmProjectsInsertion.ShowDialog = vbOK Then
                    Call LoadGrid("")
                    grdGrid.SelezionaRow(ofrmProjectsInsertion.mId)
                End If
            End If
            ofrmProjectsInsertion = Nothing

        Catch ex As Exception
            Base.ShowError.Show("Insert", Me.Name, "Insert", ex)
        Finally
            Me.Cursor = Cursors.Default
            Me.Enabled = True
        End Try
    End Sub

    Private Sub Modify() Handles objList.OnModify, grdGrid.OnModifica
        Try
            If grdGrid.CurrentRow IsNot Nothing Then
                Me.Cursor = Cursors.WaitCursor
                Me.Enabled = False

                Dim intID As Integer = Utility.ConvStrNum(grdGrid.CurrentRow.Cells("ID").Value)
                Dim ofrmProjectsModify As New frmProjectsModify
                If ofrmProjectsModify.Initialize(intID) Then
                    If ofrmProjectsModify.ShowDialog = vbOK Then
                        Call LoadGrid("")
                        grdGrid.SelezionaRow(intID)
                    End If
                End If
                ofrmProjectsModify = Nothing

            End If
        Catch ex As Exception
            Base.ShowError.Show("Modify", Me.Name, "Modify", ex)
        Finally
            Me.Cursor = Cursors.Default
            Me.Enabled = True
        End Try
    End Sub

    Private Sub Search() Handles objList.OnSearch, grdGrid.OnRicerca
        Try
            Me.Cursor = Cursors.WaitCursor
            Me.Enabled = False
            Dim ofrmProjectsearch As New frmProjectsSearch
            If ofrmProjectsearch.Initialize() Then
                If ofrmProjectsearch.ShowDialog = vbOK Then
                    Call LoadGrid(ofrmProjectsearch.strSqlWhere)
                End If
            End If
            ofrmProjectsearch = Nothing

        Catch ex As Exception
            Base.ShowError.Show("Search", Me.Name, "Search", ex)
        Finally
            Me.Cursor = Cursors.Default
            Me.Enabled = True
        End Try
    End Sub

    'Private Sub Stampa() Handles objList.OnStampa, grdGrid.OnStampa
    '    Try
    '        Me.Cursor = Cursors.WaitCursor
    '        Me.Enabled = False

    '        Dim oReportDocument As New CrystalDecisions.CrystalReports.Engine.ReportDocument
    '        oReportDocument.Load(GetSetting(Application.ProductName, "Impostazioni", "PathReport", "") & "\rpTProjects.rpt", CrystalDecisions.[Shared].OpenReportMethod.OpenReportByDefault)
    '        oReportDocument.SetDataSource(CType(grdGrid.DataSource, DataTable).DefaultView)
    '        Utility.InitializeReport(oReportDocument)
    '        oReportDocument.SetParameterValue("txtTitolo", strNameForm)
    '        oReportDocument.PrintToPrinter(1, False, 0, 0)

    '    Catch ex As Exception
    '        Base.ShowError.Show("Stampa", Me.Name, "Stampa", ex)
    '    Finally
    '        Me.Cursor = Cursors.Default
    '        Me.Enabled = True
    '    End Try
    'End Sub

    Private Sub CloseForm() Handles objList.OnClose
        Me.Close()
    End Sub

#End Region

#Region "Search"

    Private Sub objList_OnFilterSearch(ByVal inText As String) Handles objList.OnFilterSearch
        Try
            CType(grdGrid.DataSource, DataTable).DefaultView.RowFilter = String.Format("Name like '{0}%' ", inText)
        Catch ex As Exception
            Base.ShowError.Show("Rapid Search", Me.Name, "objList_OnFilterSearch", ex)
        End Try
    End Sub

#End Region

End Class